#include<bits/stdc++.h>
#define rg register
#define file(x)freopen(x".in","r",stdin);freopen(x".out","w",stdout)

using namespace std;

const long long mod=998244353;
inline long long ksm(long long a,long long b)
{
	long long re=1;
	while(b)
	{
		if(b&1)re=re*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return re;
}

const int maxn=1e7+10;

int n,m,a,b,c;
int A[maxn];
inline int f(int x)
{
	if(x>=m)x-=m;
	int re=a;
	re=1ll*re*x%m;
	if((re+=b)>=m)re-=m;
	re=1ll*re*x%m;
	if((re+=c)>=m)re-=m;
	return re+1;
}

int Tong[maxn];
int B[maxn],top;
inline bool check(double delta)
{
	double s=1.-B[1]*delta;
	for(rg int i=2;i<=top;i+=1)
	{
		s=1.0-delta*B[i]-max(0.,2*delta-s);
		if(s<0)return false;
	}
	return s>=0;
}

inline void solve()
{
	scanf("%d",&n);scanf("%d",A+0);
	scanf("%d %d %d",&a,&b,&c);scanf("%d",&m);A[0]%=m;
	a%=m,b%=m,c%=m;
	for(rg int i=1;i<=n;i+=1)A[i]=f(A[i-1]);
	for(rg int i=1;i<=m;i+=1)Tong[i]=0;
	for(rg int i=1;i<=n;i+=1)++Tong[A[i]];
	top=0;
	for(rg int i=1;i<=m;i+=1)
		if(Tong[i])B[++top]=Tong[i]>=2;
	double Left=0,Right=1.0,MID=Left;
	for(rg int Case=1;Case<=30;Case+=1)
	{
		double mid=(Left+Right)/2.0;
		if(check(mid))MID=mid,Left=mid;
		else Right=mid;
	}
	printf("%.5lf 114514\n",MID);
}

int main()
{
	file("guess");
	int T;scanf("%d",&T);
	while(T--)solve();
	return 0;
}
